{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ordinal numbering encoding\n",
    "\n",
    "**Ordinal categorical variables**\n",
    "\n",
    "Categorical variable which categories can be meaningfully ordered are called ordinal. For example:\n",
    "\n",
    "- Student's grade in an exam (A, B, C or Fail).\n",
    "- Days of the week can be ordinal with Monday = 1, and Sunday = 7.\n",
    "- Educational level, with the categories: Elementary school,  High school, College graduate, PhD ranked from 1 to 4.\n",
    "\n",
    "When the categorical variable is ordinal, the most straightforward approach is to replace the labels by some ordinal number.\n",
    "\n",
    "### Advantages\n",
    "\n",
    "- Keeps the semantical information of the variable (human readable content)\n",
    "- Straightforward\n",
    "\n",
    "### Disadvantage\n",
    "\n",
    "- Does not add machine learning valuable information\n",
    "\n",
    "I will simulate some data below to demonstrate this exercise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>day</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-11-24 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-11-23 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-11-22 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-11-21 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-11-20 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2017-11-19 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2017-11-18 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2017-11-17 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2017-11-16 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2017-11-15 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2017-11-14 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2017-11-13 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>2017-11-12 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2017-11-11 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>2017-11-10 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>2017-11-09 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>2017-11-08 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>2017-11-07 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>2017-11-06 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>2017-11-05 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>2017-11-04 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>2017-11-03 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>2017-11-02 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>2017-11-01 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>2017-10-31 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>2017-10-30 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>2017-10-29 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>2017-10-28 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>2017-10-27 23:37:17.497960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>2017-10-26 23:37:17.497960</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          day\n",
       "0  2017-11-24 23:37:17.497960\n",
       "1  2017-11-23 23:37:17.497960\n",
       "2  2017-11-22 23:37:17.497960\n",
       "3  2017-11-21 23:37:17.497960\n",
       "4  2017-11-20 23:37:17.497960\n",
       "5  2017-11-19 23:37:17.497960\n",
       "6  2017-11-18 23:37:17.497960\n",
       "7  2017-11-17 23:37:17.497960\n",
       "8  2017-11-16 23:37:17.497960\n",
       "9  2017-11-15 23:37:17.497960\n",
       "10 2017-11-14 23:37:17.497960\n",
       "11 2017-11-13 23:37:17.497960\n",
       "12 2017-11-12 23:37:17.497960\n",
       "13 2017-11-11 23:37:17.497960\n",
       "14 2017-11-10 23:37:17.497960\n",
       "15 2017-11-09 23:37:17.497960\n",
       "16 2017-11-08 23:37:17.497960\n",
       "17 2017-11-07 23:37:17.497960\n",
       "18 2017-11-06 23:37:17.497960\n",
       "19 2017-11-05 23:37:17.497960\n",
       "20 2017-11-04 23:37:17.497960\n",
       "21 2017-11-03 23:37:17.497960\n",
       "22 2017-11-02 23:37:17.497960\n",
       "23 2017-11-01 23:37:17.497960\n",
       "24 2017-10-31 23:37:17.497960\n",
       "25 2017-10-30 23:37:17.497960\n",
       "26 2017-10-29 23:37:17.497960\n",
       "27 2017-10-28 23:37:17.497960\n",
       "28 2017-10-27 23:37:17.497960\n",
       "29 2017-10-26 23:37:17.497960"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create a variable with dates, and from that extract the weekday\n",
    "# I create a list of dates with 30 days difference from today\n",
    "# and then transform it into a datafame\n",
    "\n",
    "base = datetime.datetime.today()\n",
    "date_list = [base - datetime.timedelta(days=x) for x in range(0, 30)]\n",
    "df = pd.DataFrame(date_list)\n",
    "df.columns = ['day']\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>day</th>\n",
       "      <th>day_of_week</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-11-24 23:37:17.497960</td>\n",
       "      <td>Friday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-11-23 23:37:17.497960</td>\n",
       "      <td>Thursday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-11-22 23:37:17.497960</td>\n",
       "      <td>Wednesday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-11-21 23:37:17.497960</td>\n",
       "      <td>Tuesday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-11-20 23:37:17.497960</td>\n",
       "      <td>Monday</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         day day_of_week\n",
       "0 2017-11-24 23:37:17.497960      Friday\n",
       "1 2017-11-23 23:37:17.497960    Thursday\n",
       "2 2017-11-22 23:37:17.497960   Wednesday\n",
       "3 2017-11-21 23:37:17.497960     Tuesday\n",
       "4 2017-11-20 23:37:17.497960      Monday"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# extract the week day name\n",
    "\n",
    "df['day_of_week'] = df['day'].dt.weekday_name\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>day</th>\n",
       "      <th>day_of_week</th>\n",
       "      <th>day_ordinal</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-11-24 23:37:17.497960</td>\n",
       "      <td>Friday</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-11-23 23:37:17.497960</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-11-22 23:37:17.497960</td>\n",
       "      <td>Wednesday</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-11-21 23:37:17.497960</td>\n",
       "      <td>Tuesday</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-11-20 23:37:17.497960</td>\n",
       "      <td>Monday</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2017-11-19 23:37:17.497960</td>\n",
       "      <td>Sunday</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2017-11-18 23:37:17.497960</td>\n",
       "      <td>Saturday</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2017-11-17 23:37:17.497960</td>\n",
       "      <td>Friday</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2017-11-16 23:37:17.497960</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2017-11-15 23:37:17.497960</td>\n",
       "      <td>Wednesday</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         day day_of_week  day_ordinal\n",
       "0 2017-11-24 23:37:17.497960      Friday            5\n",
       "1 2017-11-23 23:37:17.497960    Thursday            4\n",
       "2 2017-11-22 23:37:17.497960   Wednesday            3\n",
       "3 2017-11-21 23:37:17.497960     Tuesday            2\n",
       "4 2017-11-20 23:37:17.497960      Monday            1\n",
       "5 2017-11-19 23:37:17.497960      Sunday            7\n",
       "6 2017-11-18 23:37:17.497960    Saturday            6\n",
       "7 2017-11-17 23:37:17.497960      Friday            5\n",
       "8 2017-11-16 23:37:17.497960    Thursday            4\n",
       "9 2017-11-15 23:37:17.497960   Wednesday            3"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Engineer categorical variable by ordinal number replacement\n",
    "\n",
    "weekday_map = {'Monday':1,\n",
    "               'Tuesday':2,\n",
    "               'Wednesday':3,\n",
    "               'Thursday':4,\n",
    "               'Friday':5,\n",
    "               'Saturday':6,\n",
    "               'Sunday':7\n",
    "}\n",
    "\n",
    "df['day_ordinal'] = df.day_of_week.map(weekday_map)\n",
    "df.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now use the variable day_ordinal in sklearn to build machine learning models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
